{$CLEO .cs}
{$USE CLEO+}
{$USE newOpcodes}
{$USE Audio}
{$USE ini}

0000: NOP

script_name "Peds use Vends"

// By ArtemQa146


//    
repeat
wait 500
0AF0: 0@ = read_int_from_ini_file "cleo\Peds use Vends.ini" section "Models" key "Loaded"
until 0@ == 1
0AF1: write_int 0 to_ini_file "cleo\Peds use Vends.ini" section "Models" key "Loaded"

0AF0: 0@ = read_int_from_ini_file "cleo\Busy Pedestrians.ini" section "Settings" key "Peds use Vends"
if
    0@ == 0
then
    0A93: terminate_this_custom_script
end

const
    Ped_Vend = 0@
    Vend_Obj = 31@
    Find_Obj = 30@
    Find_Ped = 29@
    Progress = 28@
    Render_Obj = 27@
    Render_Model_Eat1 = 26@
    Render_Model_Eat2 = 25@
    Render_Model_Dog = 24@
    Render_Model_ice = 22@
    Render_Model_China = 21@
    Vend_Or_Cart = 23@
    Again_This_Ped = 18@
    Pause_time = 16@
    soundsqa = 15@
    Vend = 1
    Cart = 2
    Vend_crash = 3
end

//       ,    
0AF0: Render_Model_Eat1 = read_int_from_ini_file "cleo\Peds use Vends.ini" section "Models" key "Eat1"
0AF0: Render_Model_Eat2 = read_int_from_ini_file "cleo\Peds use Vends.ini" section "Models" key "Eat2"
0AF0: Render_Model_Dog = read_int_from_ini_file "cleo\Peds use Vends.ini" section "Models" key "dog"
0AF0: Render_Model_Ice = read_int_from_ini_file "cleo\Peds use Vends.ini" section "Models" key "ice"
0AF0: Render_Model_China = read_int_from_ini_file "cleo\Peds use Vends.ini" section "Models" key "ChinaEat"

//    
0AF0: Pause_time = read_int_from_ini_file "cleo\Busy Pedestrians.ini" section "Peds use Vends" key "Time for find next ped"
Pause_time *= 1000

while true
wait 250
    if
        player.Defined(0)
    then
    
        if
            not $ONMISSION == 0
        then
            continue
        end
    
    if
        001D:   timera > Pause_time  //      
    then
    
        Find_Obj = 0 //    ,     
        while 0EA9: get_any_object_no_save_recursive Find_Obj progress_to Find_Obj object_to Vend_Obj
        Vend_Or_Cart = 0
        
        if
            not  locate_char_distance_to_object $player_actor object Vend_Obj radius 40.0
        then
            continue
        end


            // .    .   
            if or
                09CC:   object Vend_Obj model_is 1775 // 
                09CC:   object Vend_Obj model_is 955 // 
                09CC:   object Vend_Obj model_is 1302 //  
                09CC:   object Vend_Obj model_is 1209 //  
                09CC:   object Vend_Obj model_is 1776 //   
                09CC:   object Vend_Obj model_is 956 //   
            then
                Vend_Or_Cart = Vend
            end
            
            //  .    
            if or
                09CC:   object Vend_Obj model_is 1340 //  
                09CC:   object Vend_Obj model_is 1342 //  
                09CC:   object Vend_Obj model_is 1341 // 
            then
                0AAA: 1@ = get_script_struct_named 'FODVEND'
                if
                    1@ > 0 //    , ..  
                then
                    Vend_Or_Cart = Cart
                end
            end
            
            
            if or
                09CC:   object Vend_Obj model_is 11691
                09CC:   object Vend_Obj model_is 11693
                09CC:   object Vend_Obj model_is 1977
            then
                0AAA: 1@ = get_script_struct_named 'COFMCH'
                0AAA: 2@ = get_script_struct_named 'SNCKMCH'
                if or
                    1@ > 0
                    2@ > 0
                then
                    Vend_Or_Cart = Vend
                end
            end
            
            //   ,   
            if or
                Vend_Or_Cart == Vend
                Vend_Or_Cart == Cart
            then
                Find_Ped = 0 //    ,     
                0400: store_coords_to 1@ 2@ 3@ from_object Vend_Obj with_offset 0.0 0.0 0.0
                0AE1: Ped_Vend = random_char_near_point 1@ 2@ 3@ in_radius 30.0 find_next 1 pass_deads 1
                if
                    056D:   actor Ped_Vend defined
                then
                    089F: get_actor Ped_Vend pedtype_to 1@
                    if or
                        1@ == 4 //  
                        1@ == 5 //  
                        1@ == 20 // 
                        1@ == 21 // 
                    then
                    
                        //      /
                        if Vend_Or_Cart == Vend
                        then 0400: store_coords_to 1@ 2@ 3@ from_object Vend_Obj with_offset 0.0 -7.0 0.0 //  
                        else 0400: store_coords_to 1@ 2@ 3@ from_object Vend_Obj with_offset 7.0 0.0 0.0 //   
                        end

                        0EFF: get_char_simplest_active_task Ped_Vend id_to 4@ pointer_to 5@
                        if and
                            80DF:   not actor Ped_Vend driving
                            803B:   not Again_This_Ped == Ped_Vend  //         
                            not is_char_script_controlled Ped_Vend
                            0EEA: locate_char_distance_to_coordinates Ped_Vend pos 1@ 2@ 3@ radius 7.0
                            4@ == 900 //  
                        then
                        
                            if
                                Vend_Or_Cart == Vend
                            then
                                089F: get_actor Ped_Vend pedtype_to 1@
                                if or
                                    1@ == 20
                                    1@ == 21
                                then
                                    Vend_Or_Cart = Vend_crash
                                end
                                if
                                    1@ == 4
                                then
                                    if
                                        0E4D: random_percent 15
                                    then
                                        Vend_Or_Cart = Vend_crash
                                    end
                                end
                            end
                        
                            //  
                            repeat
                            wait 0
                            04ED: load_animation  "VENDING"
                            until 04EE:   animation "VENDING" loaded
                            
                            repeat
                            wait 0
                            04ED: load_animation  "GANGS"
                            until 04EE:   animation "GANGS" loaded
                            Progress = 0
                            timera = 0
                            
                            soundsqa = -1
                            
                                repeat
                                wait 0
                                
                                //     
                                if and
                                    056D:   actor Ped_Vend defined
                                    03CA:   object Vend_Obj exists
                                then
                                
                                if
                                    not soundsqa == -1
                                then
                                    04C4: store_coords_to 1@ 2@ 3@ from_actor Ped_Vend with_offset 0.0 0.0 0.0
                                    0EB2: get_offset_from_camera_in_world_coords 0.0 0.0 0.0 store_to 4@ 10@ 11@
                                    050A: 1@ = distance_between_XYZ 1@ 2@ 3@ and_XYZ 4@ 10@ 11@
                                    1@ /= 100.0
                                    2@ = 0.5
                                    0063: 2@ -= 1@  // (float)
                                    if
                                        2@ < 0.0
                                    then
                                        2@ = 0.0
                                    end
                                    0ABC: set_audio_stream soundsqa volume 2@
                                end

                                    //      ,    
                                    if or
                                        not locate_char_distance_to_object Ped_Vend object Vend_Obj radius 20.0
                                        timera > 30000 //     -   ,    30 
                                        0118:   actor Ped_Vend dead
                                        is_char_script_controlled Ped_Vend
                                    then
                                        break
                                    end
                                    
                                    //       ---------------------------------------------------------------------------------------

                                    //      
                                    if and
                                        Vend_Or_Cart == Vend_crash
                                        Progress == 0
                                    then
                                        Progress = 1
                                        0400: store_coords_to 1@ 2@ 3@ from_object Vend_Obj with_offset 0.0 -1.0 0.5
                                        0176: 4@ = object Vend_Obj Z_angle
                                        0209: 5@ = random_int_in_ranges 1 4
                                        if
                                            5@ == 1
                                        then
                                            0804: AS_actor Ped_Vend walk_to 1@ 2@ 3@ angle 4@ radius 0.4 animation "shake_cara" IFP_file "GANGS" rate 2.0 LA 0 LX 0 LY 0 LF 0 LT 0
                                            0AD3: 5@v = string_format "shake_cara"
                                            continue
                                        end
                                        if
                                            5@ == 2
                                        then
                                            0804: AS_actor Ped_Vend walk_to 1@ 2@ 3@ angle 4@ radius 0.4 animation "shake_carK" IFP_file "GANGS" rate 2.0 LA 0 LX 0 LY 0 LF 0 LT 0
                                            0AD3: 5@v = string_format "shake_carK"
                                            continue
                                        end
                                        if
                                            5@ == 3
                                        then
                                            0804: AS_actor Ped_Vend walk_to 1@ 2@ 3@ angle 4@ radius 0.4 animation "shake_carSH" IFP_file "GANGS" rate 2.0 LA 0 LX 0 LY 0 LF 0 LT 0
                                            0AD3: 5@v = string_format "shake_carSH"
                                            continue
                                        end
                                    end
                                    
                                    //      ,     
                                    if and
                                        Vend_Or_Cart == Vend_crash
                                        Progress == 1
                                    then
                                        if
                                            0611:   actor Ped_Vend performing_animation 5@v
                                        then
                                            0613: 1@ = actor Ped_Vend animation 5@v time
                                            if
                                                0611:   actor Ped_Vend performing_animation "shake_carSH"
                                            then
                                                if and
                                                    soundsqa == -1
                                                    1@ > 0.45
                                                    1@ < 0.5
                                                then
                                                    0AC1: soundsqa = load_audio_stream_with_3d_support "SQ\pedspunch.mp3" // IF and SET
                                                    0AC4: link_3d_audio_stream soundsqa to_actor Ped_Vend
                                                    0AAD: set_audio_stream soundsqa state 1
                                                    0ABC: set_audio_stream soundsqa volume 0.4
                                                end
                                            end
                                            if
                                                0611:   actor Ped_Vend performing_animation "shake_carK"
                                            then
                                                if and
                                                    soundsqa == -1
                                                    1@ > 0.4
                                                    1@ < 0.45
                                                then
                                                    0AC1: soundsqa = load_audio_stream_with_3d_support "SQ\pedspunch.mp3" // IF and SET
                                                    0AC4: link_3d_audio_stream soundsqa to_actor Ped_Vend
                                                    0AAD: set_audio_stream soundsqa state 1
                                                    0ABC: set_audio_stream soundsqa volume 0.4
                                                end
                                            end
                                            if
                                                0611:   actor Ped_Vend performing_animation "shake_cara"
                                            then
                                                if and
                                                    soundsqa == -1
                                                    1@ > 0.25
                                                    1@ < 0.3
                                                then
                                                    0AC1: soundsqa = load_audio_stream_with_3d_support "SQ\pedspunch.mp3" // IF and SET
                                                    0AC4: link_3d_audio_stream soundsqa to_actor Ped_Vend
                                                    0AAD: set_audio_stream soundsqa state 1
                                                    0ABC: set_audio_stream soundsqa volume 0.4
                                                end
                                            end
                                            if
                                                1@ >= 0.95
                                            then
                                                0209: 1@ = random_int_in_ranges 1 3
                                                if
                                                    1@ == 1
                                                then
                                                    break
                                                else
                                                    Progress = 2
                                                    Vend_Or_Cart = Vend
                                                end
                                            end
                                        end
                                    end
                                
                                //      ---------------------------------------------------------------------------------------
                                    
                                    //      
                                    if and
                                        Vend_Or_Cart == Vend
                                        Progress == 0
                                    then
                                        0400: store_coords_to 1@ 2@ 3@ from_object Vend_Obj with_offset 0.0 -1.0 0.5
                                        0176: 4@ = object Vend_Obj Z_angle
                                        0804: AS_actor Ped_Vend walk_to 1@ 2@ 3@ angle 4@ radius 0.4 animation "VEND_USE" IFP_file "VENDING" rate 2.0 LA 0 LX 0 LY 0 LF 0 LT 0
                                        Progress = 1
                                    end
                                    
                                    //      ,     
                                    if and
                                        Vend_Or_Cart == Vend
                                        Progress == 1
                                    then
                                        if
                                            0611:   actor Ped_Vend performing_animation "VEND_USE"
                                        then
                                            0613: 1@ = actor Ped_Vend animation "VEND_USE" time
                                            if and
                                                1@ < 0.2
                                                soundsqa == -1
                                            then
                                                0AC1: soundsqa = load_audio_stream_with_3d_support "SQ\pedsbuy.mp3" // IF and SET
                                                0AC4: link_3d_audio_stream soundsqa to_actor Ped_Vend
                                                0AAD: set_audio_stream soundsqa state 1
                                                0ABC: set_audio_stream soundsqa volume 0.4
                                            end
                                            if
                                                1@ >= 0.95
                                            then
                                                Progress = 2
                                            end
                                        end
                                    end
                                    
                                    //   
                                    if and
                                        Vend_Or_Cart == Vend
                                        Progress == 2
                                    then
                                        if or
                                            09CC:   object Vend_Obj model_is 1776
                                            09CC:   object Vend_Obj model_is 956
                                            09CC:   object Vend_Obj model_is 1977
                                        then
                                            //       
                                            0209: 1@ = random_int_in_ranges 1 3
                                            if 1@ == 1
                                            then 0085: 1@ = Render_Model_Eat1 // (int)
                                            else 0085: 1@ = Render_Model_Eat2 // (int)
                                            end
                                            0F02: create_render Ped_Vend special_model 1@ bone 34 offset 0.09 0.05 0.01 rotation 0 90.0 180.0 store_to Render_Obj
                                            0E37: set_render_object_scale Render_Obj 0.7 0.7 0.7
                                            0AD3: 5@v = string_format "VEND_EAT1_P"
                                            0812: AS_actor Ped_Vend perform_animation 5@v IFP "VENDING" framedelta 2.0 loopA 0 lockX 0 lockY 0 lockF 0 time 0  // versionB
                                            Progress = 3
                                            timerb = 0
                                        else
                                            //       
                                            if or
                                                09CC:   object Vend_Obj model_is 11691
                                                09CC:   object Vend_Obj model_is 11693
                                            then
                                                0AF0: 2@ = read_int_from_ini_file "cleo\vending.ini" section "Coffee" key "ModelID3"
                                                model.Load(2@)
                                                038B: load_requested_models
                                                0E2E: create_render_object_to_char_bone Ped_Vend model 2@ bone 34 offset 0.08 0.01 0.0 rotation 90.0 0 0 store_to Render_Obj
                                                model.Destroy(2@)
                                            else
                                                gosub @Sprunk_bottle
                                            end
                                            0AD3: 5@v = string_format "VEND_DRINK_P"
                                            0812: AS_actor Ped_Vend perform_animation 5@v IFP "VENDING" framedelta 2.0 loopA 0 lockX 0 lockY 0 lockF 0 time 0  // versionB
                                            Progress = 3
                                            timerb = 0
                                        end
                                    end
                                    
                                    //      
                                    if and
                                        Vend_Or_Cart == Vend
                                        Progress == 3
                                    then
                                        if
                                            0611:   actor Ped_Vend performing_animation 5@v
                                        then
                                            0613: 1@ = actor Ped_Vend animation 5@v time
                                            if
                                                1@ >= 0.9
                                            then
                                                0226: 1@ = actor Ped_Vend health
                                                1@ += 10
                                                0223: set_actor Ped_Vend health_to 1@
                                                0E2F: delete_render_object Render_Obj
                                                Progress = 4
                                            end
                                        else
                                            if
                                                timerb > 7000
                                            then
                                                0E2F: delete_render_object Render_Obj
                                                Progress = 4
                                            end
                                        end
                                    end
                                    
                                    //      
                                    if and
                                        Vend_Or_Cart == Vend
                                        Progress == 4
                                    then
                                        if
                                            8611:   not actor Ped_Vend performing_animation 5@v
                                        then
                                            if
                                                0E42: is_char_doing_task_id Ped_Vend 934 //     ,      
                                            then
                                                0792: disembark_instantly_actor Ped_Vend
                                                break
                                            else
                                                break
                                            end
                                        end
                                    end
                                    
                                    
                                    
                                //  ,    ---------------------------------------------------------------------------------------
                                    
                                    //      ,     
                                    if and
                                        Vend_Or_Cart == Cart
                                        Progress == 0
                                    then
                                        0400: store_coords_to 1@ 2@ 3@ from_object Vend_Obj with_offset 1.1 0.0 0.5
                                        0176: 4@ = object Vend_Obj Z_angle
                                        4@ += 90.0
                                        0804: AS_actor Ped_Vend walk_to 1@ 2@ 3@ angle 4@ radius 0.0 animation "VEND_EAT1_P" IFP_file "VENDING" rate 2.0 LA 0 LX 0 LY 0 LF 0 LT 0
                                        Progress = 1
                                        timerb = 0
                                    end
                                    
                                    //   
                                    if and
                                        Vend_Or_Cart == Cart
                                        Progress == 1
                                    then
                                        if
                                            0611:   actor Ped_Vend performing_animation "VEND_EAT1_P"
                                        then
                                            if 09CC:   object Vend_Obj model_is 1340 //  
                                            then 0085: 1@ = Render_Model_Dog // (int)
                                            end
                                            if 09CC:   object Vend_Obj model_is 1342 //  
                                            then 0085: 1@ = Render_Model_China // (int)
                                            end
                                            if 09CC:   object Vend_Obj model_is 1341 // 
                                            then 0085: 1@ = Render_Model_ice // (int)
                                            end
                                            0F02: create_render Ped_Vend special_model 1@ bone 34 offset 0.09 0.05 0.01 rotation 0 90.0 180.0 store_to Render_Obj
                                            0E37: set_render_object_scale Render_Obj 0.8 0.8 0.8
                                            Progress = 2
                                            timerb = 0
                                        end
                                    end
                                    
                                    //      
                                    if and
                                        Vend_Or_Cart == Cart
                                        Progress == 2
                                    then
                                        if
                                            0611:   actor Ped_Vend performing_animation "VEND_EAT1_P"
                                        then
                                            0613: 1@ = actor Ped_Vend animation "VEND_EAT1_P" time
                                            if
                                                1@ >= 0.7
                                            then
                                                0226: 1@ = actor Ped_Vend health
                                                1@ += 10
                                                0223: set_actor Ped_Vend health_to 1@
                                                0E2F: delete_render_object Render_Obj
                                                Progress = 3
                                            end
                                        else
                                            if
                                                timerb > 10000
                                            then
                                                0E2F: delete_render_object Render_Obj
                                                Progress = 3
                                            end
                                        end
                                    end
                                    
                                    //      
                                    if and
                                        Vend_Or_Cart == Cart
                                        Progress == 3
                                    then
                                        if
                                            8611:   not actor Ped_Vend performing_animation "VEND_EAT1_P"
                                        then
                                            if
                                                0E42: is_char_doing_task_id Ped_Vend 934 //     ,      
                                            then
                                                0792: disembark_instantly_actor Ped_Vend
                                                break
                                            else
                                                break
                                            end
                                        end
                                    end
                                    
                                //      
                                else
                                    break
                                end
                                until not player.Defined(0)
                                04EF: release_animation "VENDING"
                                04EF: release_animation "GANGS"
                                if not soundsqa == -1
                                then 0AAE: remove_audio_stream soundsqa
                                end
                                0085: Again_This_Ped = Ped_Vend // //         
                                timera = 0
                                break
                            end
                        end
                    end
                end
            end
        end
    end
end

:Sprunk_bottle
model.Load(2601)
038B: load_requested_models
07E4: get_model 2601 dimensions_cornerA_to 1@ 2@ 3@ dimensions_cornerB_to 1@ 2@ 3@
0015: 2@ /= 2.0
0009: 2@ += 0.027
0E2E: create_render_object_to_char_bone Ped_Vend model 2601 bone 34 offset 0.062 2@ 0.0 rotation 0 0 0 store_to Render_Obj
0E37: set_render_object_scale Render_Obj 0.8 0.8 0.8
model.Destroy(2601)
return